//
//  SearchViewController.m
//  AffectionBee
//
//  Created by 李通 on 16/8/18.
//  Copyright © 2016年 chivalrous. All rights reserved.
//

#import "SearchViewController.h"
#import "FirstCellModel.h"
#import "SearchWordsModel.h"



@interface SearchViewController ()

//(1)搜索关键字字符串数组
@property (nonatomic,strong)NSArray<NSString *> *SearchWords;

//(2)搜索结果数据数组
@property (nonatomic,strong)NSArray<FirstCellModel *> *searchArray;


@end

@implementation SearchViewController


//懒加载关键字字符串数组
-(NSArray<NSString *> *)SearchWords
{
    if (_SearchWords == nil)
    {
        
        [SearchWordsModel GetSearchWordsSucess:^(NSArray<NSString *> *strArray) {
            
            _SearchWords = strArray;
            
            [strArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                NSLog(@"%@",obj);
                
            }];
            
        } Error:^{
            
            NSLog(@"网络获取失败");
            
        }];
        
    }
    
    return _SearchWords;
}

//懒加载搜索结果数组
-(NSArray <FirstCellModel *> *)searchArray
{
    if (_searchArray == nil)
    {
        
        //(1)urlString
        NSString *urlString = @"http://iosapi.itcast.cn/loveBeen/promotion.json.php";
        
        //(2)参数
        NSDictionary *param = @{@"call":@8};
        
        
        //(3)直接POST请求
        [FirstCellModel GetFirstCellDataArrayWithUrl:urlString andParameter:param Sucess:^(NSArray<FirstCellModel *> *dataArray) {
            
            _searchArray = dataArray;
            
            
            [dataArray enumerateObjectsUsingBlock:^(FirstCellModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
               
                
                NSLog(@"%@",obj.name);
                NSLog(@"%@",obj.brand_name);
            }];
            
            
        } Error:^{
            
            NSLog(@"网络获取失败");
            
            
        }];
        
    }
    
    return _searchArray;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UISearchBar *search = [[UISearchBar alloc]init];
    search.placeholder = @"请输入商品名称";
    self.navigationItem.titleView = search;
    
    //测试数据
    //NSLog(@"%@",self.SearchWords);
    //NSLog(@"%@",self.searchArray);
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



@end
